package bytes;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Description
 * @Author zhaobaolin
 * @Date 2018/11/10
 */
public class ByteArrayOutputStreamExample {
    public static void main(String[] args) {
        demo();
    }

    private static void demo()
    {
        String source = "1234567890";
        ByteArrayOutputStream out1 = new ByteArrayOutputStream();
        byte[] b = source.getBytes();
        try{
            out1.write(b);
        }catch (IOException e){
            e.printStackTrace();
        }
        System.out.println(out1.toString());//输出：1234567890

        byte[] bb = out1.toByteArray();
        for(int i=0;i<bb.length;i++){
            System.out.print((char)bb[i]);
        }
        //输出：1234567890
        System.out.println();

        try(FileOutputStream f = new FileOutputStream("./io/src/bytes/f.txt")){
            out1.writeTo(f);//把out1的内容写入f
        }catch (IOException e){
            e.printStackTrace();
        }

        out1.reset();
        for(int i=0;i<3;i++){
            out1.write('X');
        }
        System.out.println(out1.toString());//输出：XXX
    }

}
